home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1997 / HAM Radio 1997.iso / vcls / lan / winlan.pas < prev   
Pascal/Delphi Source File  |  1996-04-08  |  11KB  |  303 lines

  1. unit WinLan;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs;
  8.  
  9. type
  10.   EWinLan = class(Exception);  { exception used in TWinLan }
  11.  
  12.   TWinLan = class(TComponent)
  13.   private
  14.     { Private declarations }
  15.     FMachineName,
  16.     FUserName:String;
  17.     Procedure RaiseError(Error:Word);
  18.     Function GetWorkStationInfo(GetType:Word):String;
  19.   protected
  20.     { Protected declarations }
  21.     Function GetMachineName:String;
  22.     Function GetUserName:String;
  23.   public
  24.     { Public declarations }
  25.     Function FindDisk:String;
  26.     Function FindPrinter:String;
  27.     Procedure ConnectDisk;
  28.     Procedure ConnectPrinter;
  29.     Procedure DisconnectDisk;
  30.     Procedure DisconnectPrinter;
  31.     Function FindMachine:String;
  32.     Procedure ChangePassword;
  33.     Function AutoLogon:Boolean;
  34.     Function Logoff:Boolean;
  35.     Function AddConnection(Const NetPath,Password,LocalName:String):Boolean;
  36.     Function CancelConnection(Const LocalName:String; ForceIfOpenFiles:Boolean):Boolean;
  37.   published
  38.     { Published declarations }
  39.     property MachineName:String read GetMachineName;
  40.     property UserName:String read GetUserName;
  41.   end;
  42.  
  43. procedure Register;
  44.  
  45. implementation
  46.  
  47. Const WNBD_CONN_UNKNOWN       =0;
  48.       WNBD_CONN_DISKTREE      =1;
  49.       WNBD_CONN_PRINTQ        =3;
  50.       WNBD_MAX_LENGTH         =$80;
  51.       NERR_SUCCESS  =0;
  52.  
  53.       WKSTAINFO_COMPUTER = 0;
  54.       WKSTAINFO_USER     = 1;
  55.  
  56. Function WNetGetCaps(w:Word):THandle; far; external 'USER';
  57. Function WNetBrowseDialog(hwndParent:HWnd; nType:Word; szPath:PChar):Word; far;
  58.          external 'WFWNET.DRV';
  59. Function WNetConnectDialog(hwndOwner:Hwnd; iType:Word):Word; far;
  60.          external 'WFWNET.DRV';
  61. Function WNetDisconnectDialog(hwndOwner:Hwnd; iType:Word):Word; far;
  62.          external 'WFWNET.DRV';
  63. Function WNetServerBrowseDialog(hwndParent:HWnd; lpszSectionName, lpszBuffer:PChar; cbBuffer:Word; flFlags:Longint):Word; far;
  64.          external 'WFWNET.DRV';
  65. Procedure I_ChangeCachePassword(hwndOwner:HWnd); far; external 'WFWNET.DRV';
  66. Procedure I_ChangePassword(hwndOwner:HWnd); far; external 'WFWNET.DRV';
  67. Function I_AutoLogon(hwndOwner:HWnd; lpszReserved:PChar; fPrompt:Bool; pfLoggedOn:PBool):Bool; far;
  68.          external 'WFWNET.DRV';
  69. Function I_Logoff(hwndOwner:HWnd; lpszReserved:PChar):Bool; far;
  70.          external 'WFWNET.DRV';
  71. Function NetWkstaGetInfo( pszServer:PChar;
  72.                           sLevel:Integer;
  73.                           pbBuffer:PChar;
  74.                           cbBuffer:Word;
  75.                           pcbTotalAvail:PWord):Word; far; external 'NETAPI';
  76. Function WNetGetErrorText(nError:Word;  lpszText:Pchar; cbText:Word):Word; far;
  77.          external 'WFWNET.DRV';
  78.  
  79. Type TString=Array[0..255] of Char;
  80.      PWksta_info_0=^TWksta_info_0;
  81.      Twksta_info_0=Record
  82.         wki0_reserved_1:Word;    { reserved; must be zero }
  83.         wki0_reserved_2:Longint;    { reserved; must be zero }
  84.         wki0_root:PChar;    { path to network directory }
  85.         wki0_computername:PChar;    { name of computer }
  86.         wki0_username:PChar;    { name of user logged on }
  87.         wki0_langroup:PChar;    { name of workgroup }
  88.         wki0_ver_major:Byte;    { major version number }
  89.         wki0_ver_minor:Byte;    { minor version number }
  90.         wki0_reserved_3:Longint;    { reserved; must be zero }
  91.         wki0_charwait:Word;    { reserved; must be zero }
  92.         wki0_chartime:Longint;    { reserved; must be zero }
  93.         wki0_charcount:Word;    { reserved; must be zero }
  94.         wki0_reserved_4:Word;    { reserved; must be zero }
  95.         wki0_reserved_5:Word;    { reserved; must be zero }
  96.         wki0_keepconn:Word;    { maximum time to keep inactive connection }
  97.         wki0_keepsearch:Word;    { maximum time to keep inactive search }
  98.         wki0_maxthreads:Word;    { reserved; must be zero }
  99.         wki0_maxcmds:Word;    { maximum simultaneous network connections }
  100.         wki0_reserved_6:Word;    { reserved; must be zero }
  101.         wki0_numworkbuf:Word;    { internal work buffers }
  102.         wki0_sizworkbuf:Word;    { size of work buffer, in bytes }
  103.         wki0_maxwrkcache:Word;    { reserved; must be zero }
  104.         wki0_sesstimeout:Word;    { reserved; must be zero }
  105.         wki0_sizerror:Word;    { reserved }
  106.         wki0_numalerts:Word;    { reserved }
  107.         wki0_numservices:Word;    { reserved }
  108.         wki0_errlogsz:Word;    { reserved }
  109.         wki0_printbuftime:Word;    { reserved }
  110.         wki0_numcharbuf:Word;    { reserved }
  111.         wki0_sizcharbuf:Word;    { reserved }
  112.         wki0_logon_server:PChar;    { reserved }
  113.         wki0_wrkheuristics:PChar;    { reserved }
  114.         wki0_mailslots:Word;    { mailslot flag }
  115.      End;
  116.      PWksta_info_1=^TWksta_info_1;
  117.      Twksta_info_1=Record
  118.         wki1_reserved_1:Word;    { reserved; must be zero }
  119.         wki1_reserved_2:Longint;    { reserved; must be zero }
  120.         wki1_root:PChar;    { path to network directory }
  121.         wki1_computername:PChar;    { name of computer }
  122.         wki1_username:PChar;    { name of user logged on }
  123.         wki1_langroup:PChar;    { name of workgroup }
  124.         wki1_ver_major:Byte;    { major version number }
  125.         wki1_ver_minor:Byte;    { minor version number }
  126.         wki1_reserved_3:Longint;    { reserved; must be zero }
  127.         wki1_charwait:Word;    { reserved; must be zero }
  128.         wki1_chartime:Longint;    { reserved; must be zero }
  129.         wki1_charcount:Word;    { reserved; must be zero }
  130.         wki1_reserved_4:Word;    { reserved; must be zero }
  131.         wki1_reserved_5:Word;    { reserved; must be zero }
  132.         wki1_keepconn:Word;    { maximum time to keep inactive connection }
  133.         wki1_keepsearch:Word;    { maximum time to keep inactive search }
  134.         wki1_maxthreads:Word;    { reserved; must be zero }
  135.         wki1_maxcmds:Word;    { maximum simultaneous network connections }
  136.         wki1_reserved_6:Word;    { reserved; must be zero }
  137.         wki1_numworkbuf:Word;    { internal work buffers }
  138.         wki1_sizworkbuf:Word;    { size of work buffer, in bytes }
  139.         wki1_maxwrkcache:Word;    { reserved; must be zero }
  140.         wki1_sesstimeout:Word;    { reserved; must be zero }
  141.         wki1_sizerror:Word;    { reserved }
  142.         wki1_numalerts:Word;    { reserved }
  143.         wki1_numservices:Word;    { reserved }
  144.         wki1_errlogsz:Word;    { reserved }
  145.         wki1_printbuftime:Word;    { reserved }
  146.         wki1_numcharbuf:Word;    { reserved }
  147.         wki1_sizcharbuf:Word;    { reserved }
  148.         wki1_logon_server:PChar;    { reserved }
  149.         wki1_wrkheuristics:PChar;    { reserved }
  150.         wki1_mailslots:Word;    { mailslot flag }
  151.         wki1_logon_domain:PChar;    { name of logon workgroup }
  152.         wki1_oth_domains:Word;    { reserved }
  153.         wki1_numdgrambuf:Word;    { reserved }
  154.      End;
  155.      PWkSta_info_10=^TWkSta_info_10;
  156.      Twksta_info_10=Record
  157.         wki10_computername:PChar;    { name of computer }
  158.         wki10_username:PChar;    { name of logged on user}
  159.         wki10_langroup:PChar;    { name of workgroup}
  160.         wki10_ver_major:Byte;    { major version number}
  161.         wki10_ver_minor:Byte;    { minor version number }
  162.         wki10_logon_domain:PChar;    { name of logon workgroup}
  163.         wki10_oth_domains:PChar;    { reserved; must be zero  }
  164.      End;
  165.  
  166. Function TWinLan.FindDisk:String;
  167. Var St:TString;
  168.     Res:Word;
  169. Begin
  170.   result:='';
  171.   Res:=WNetBrowseDialog(0,WNBD_CONN_DISKTREE,St);
  172.   if Res=NERR_SUCCESS then result:=StrPas(St)
  173.                       else if Res<>12 then RaiseError(Res);
  174. End;
  175.  
  176. Function TWinLan.FindPrinter:String;
  177. Var St:TString;
  178.     Res:Word;
  179. Begin
  180.   result:='';
  181.   Res:=WNetBrowseDialog(0,WNBD_CONN_PRINTQ,St);
  182.   if Res=NERR_SUCCESS then result:=StrPas(St)
  183.                       else if Res<>12 then RaiseError(Res);
  184. End;
  185.  
  186. Procedure TWinLan.ConnectDisk;
  187. Begin
  188.   WNetConnectDialog(0,WNBD_CONN_DISKTREE);
  189. End;
  190.  
  191. Procedure TWinLan.ConnectPrinter;
  192. Begin
  193.   WNetConnectDialog(0,WNBD_CONN_PRINTQ);
  194. End;
  195.  
  196. Procedure TWinLan.DisconnectDisk;
  197. Begin
  198.   WNetDisconnectDialog(0,WNBD_CONN_DISKTREE);
  199. End;
  200.  
  201. Procedure TWinLan.DisconnectPrinter;
  202. Begin
  203.   WNetDisconnectDialog(0,WNBD_CONN_PRINTQ);
  204. End;
  205.  
  206. Function TWinLan.FindMachine:String;
  207. Var St:TString;
  208.     Res:Word;
  209. Begin
  210.   Result:='';
  211.   Res:=WNetServerBrowseDialog(0,nil,St,Sizeof(St),0);
  212.   if Res=NERR_SUCCESS then result:=StrPas(St)
  213.                       else if Res<>12 then RaiseError(Res);
  214. End;
  215.  
  216. Procedure TWinLan.ChangePassword;
  217. Begin
  218.   I_ChangeCachePassword(0);
  219. End;
  220.  
  221. Function TWinLan.AutoLogon:Boolean;
  222. Var WasLogged:PBool;
  223. Begin
  224.   I_AutoLogon(0,nil,True,@WasLogged);
  225.   result:=WasLogged^;
  226. End;
  227.  
  228. Function TWinLan.Logoff:Boolean;
  229. Begin
  230.   result:=I_Logoff(0,nil);
  231. End;
  232.  
  233. Function TWinLan.GetWorkStationInfo(GetType:Word):String;
  234. Const Size=60000;
  235. Var TotalSize,Res:Word;
  236.     Pw:PChar;
  237. Begin
  238.   GetMem(pw,Size);
  239.   Res:=NetWkstaGetInfo(nil,10,pw,Size,@TotalSize);
  240.   try
  241.     if Res=NERR_SUCCESS Then
  242.     Begin
  243.       Case GetType of
  244.          WKSTAINFO_COMPUTER: Result:=StrPas(PWksta_info_10(PW)^.wki10_ComputerName);
  245.          WKSTAINFO_USER    : Result:=StrPas(PWksta_info_10(PW)^.wki10_UserName);
  246.       else
  247.          Raise EWinLan.Create('Not available Workstation information.');
  248.       End;
  249.     End
  250.     else RaiseError(Res);
  251.   finally
  252.     FreeMem(pw,Size);
  253.   end;
  254. End;
  255.  
  256. Function TWinLan.GetMachineName:String;
  257. Begin
  258.   result:=GetWorkStationInfo(WKSTAINFO_COMPUTER);
  259. End;
  260.  
  261. Function TWinLan.GetUserName:String;
  262. Begin
  263.   result:=GetWorkStationInfo(WKSTAINFO_USER);
  264. End;
  265.  
  266. Function TWinLan.AddConnection(Const NetPath,Password,LocalName:String):Boolean;
  267. Var S1,S2,S3:TString;
  268.     Res:Word;
  269. Begin
  270.   Result:=False;
  271.   Res:=WNetAddConnection( StrPcopy(S1,NetPath),
  272.                           StrPCopy(S2,Password),
  273.                           StrPCopy(S3,LocalName));
  274.   if Res=NERR_SUCCESS then result:=True else RaiseError(Res);
  275. End;
  276.  
  277. Function TWinLan.CancelConnection(Const LocalName:String; ForceIfOpenFiles:Boolean):Boolean;
  278. Var S1:TString;
  279.     Res:Word;
  280. Begin
  281.   Result:=False;
  282.   Res:=WNetCancelConnection(StrPcopy(S1,LocalName),ForceIfOpenFiles);
  283.   if Res=NERR_SUCCESS then result:=True else RaiseError(Res);
  284. End;
  285.  
  286. Procedure TWinLan.RaiseError(Error:Word);
  287. Var SError:TString;
  288.     StError:String;
  289. Begin
  290.   if WNetGetErrorText(Error,SError,SizeOf(SError))=0 then
  291.      StError:=StrPas(SError)
  292.   else
  293.      StError:='WinLan error: '+IntToStr(Error);
  294.   Raise EWinLan.Create(StError);
  295. End;
  296.  
  297. procedure Register;
  298. begin
  299.   RegisterComponents('Samples', [TWinLan]);
  300. end;
  301.  
  302. end.
  303.